package com.breaking.second.activity.pager.home;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson2.JSONObject;
import com.breaking.second.activity.pager.BaseFragment;
import com.breaking.second.activity.pager.adapter.GoodsAdapter;
import com.breaking.second.databinding.PagerGoodsBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.GoodsEntity;
import com.breaking.second.entity.GoodsTypeEntity;
import com.breaking.second.entity.PageableEntity;
import com.breaking.second.utils.compat.FormBodyCompat;
import com.breaking.second.utils.compat.RefreshLayoutCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

//商品展示界面
public class GoodsPager extends BaseFragment<PagerGoodsBinding> {
    private final List<GoodsEntity> goodsEntities = new ArrayList<>();
    private GoodsAdapter adapter;
    private int i = 0;

    public GoodsPager(GoodsTypeEntity goodsTypeEntity) {
        pageableVo.setTypeId(goodsTypeEntity.getId());
    }

    public GoodsPager(GoodsTypeEntity goodsTypeEntity, int i) {
        pageableVo.setTypeId(goodsTypeEntity.getId());
        this.i = i;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initSmart();
        initRecycler();
        if (i == 0) {
            initData();
        }
    }

    private void initRecycler() {
        adapter = new GoodsAdapter(requireContext(), goodsEntities);
        binding.recycler.setLayoutManager(new LinearLayoutManager(requireContext()));
        binding.recycler.setAdapter(adapter);
    }

    private void initSmart() {
        RefreshLayoutCompat.setRefresh(pageableVo, binding.smart, this::onRefresh);
    }

    private void onRefresh(RefreshLayout refreshLayout) {
        getGoodsData();
    }

    public void initData() {
        getGoodsData();
    }

    private void getGoodsData() {
        FormBody.Builder form = FormBodyCompat.to(pageableVo);
        OkhttpUtil.Get("goods/all", form, new OkhttpUtil.CallbackListener(this::getGoodsData));
    }

    private void getGoodsData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            PageableEntity pageable = bookEntity.to(PageableEntity.class);

            if (pageable == null) return;
            if (pageable.getSize() != pageable.getSize()) {//禁止加载更多
                binding.smart.finishLoadMoreWithNoMoreData();
            }
            if (pageable.getTotal() == 0) {
                Toast.makeText(requireContext(), "不存在数据", Toast.LENGTH_SHORT).show();
            }

            List<GoodsEntity> goodsEntities = pageable.toList(GoodsEntity.class);
            if (goodsEntities == null) return;
            if (goodsEntities.isEmpty()) return;
            if (pageable.getCurrent() == 0) {
                adapter.notifyItemRangeRemoved(0, this.goodsEntities.size() + 1);
                this.goodsEntities.clear();
            }

            int start = this.goodsEntities.size();
            this.goodsEntities.addAll(goodsEntities);
            adapter.notifyItemRangeChanged(start, this.goodsEntities.size());

        }
    }

    public void search(String text) {
        pageableVo.setKeyword(text);
        adapter.notifyItemRangeRemoved(0, goodsEntities.size() + 1);
        goodsEntities.clear();
        getGoodsData();
    }
}
